﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace TestForm.sql
{
    public class SqlServerAgent
    {
        protected static SqlServerAgent sqlAgent = new SqlServerAgent();
        SqlConnection sqlconnection = null;
        protected SqlServerAgent()
        {
            String connString = "server=127.0.0.1;database =MyTaobao;uid =sa;pwd=mn141319";
            sqlconnection = new SqlConnection(connString);
            sqlconnection.Open();
        }
        protected SqlCommand getCommand_internal()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "";
            cmd.Connection = this.sqlconnection;
            return cmd;
        }

        public static SqlParameter createParam(Object v)
        {
            SqlParameter p = new SqlParameter();
            p.Value = v;
            return p;
        }

        public static  SqlCommand getCommand()
        {
            return sqlAgent.getCommand_internal();
        }
        public static void executeInsert(String sql, ArrayList ps)
        {

            SqlCommand cmd = getCommand();
            cmd.CommandText = sql;
            foreach (Object v in ps)
            {
                cmd.Parameters.Add(createParam(v));
            }
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            cmd.Dispose();
        }

        ~SqlServerAgent()
        {
            sqlconnection.Close();
        }


    }
}
